package com.yuanli.yuanliutils.yl_permission;

import android.app.Activity;


import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;

import java.util.List;


/**
 * shiys
 * 权限工具类
 */
public class RxPermissionsUtils {

    /**
     * 申请权限
     * 例如：RxPermissionsUtils.requestPermission(this, new String[]{Manifest.permission.CAMERA}, "相机权限", new RxPermissionsUtils.Accept() {

     })
     * @param activity 上下文
     * @param permissions 权限数组
     * @param permissionsDesc   权限描述
     * @param accept      成功回调
     */
    public static void requestPermission(Activity activity, String[] permissions,String permissionsDesc, Accept accept) {
        XXPermissions.with(activity)
                .permission(permissions)
                .interceptor(new PermissionInterceptor(permissionsDesc))
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean allGranted) {
                        if (allGranted){
                            //申请的权限全部允许
                            accept.accept();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean doNotAskAgain) {
                        OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                    }
                });


    }


    public interface Accept {
        void accept(); // 只需要返回成功的
    }
}
